package com.example.service.impl;

import cn.hutool.extra.tokenizer.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mapper.ContactsGroupDetailMapper;
import com.example.pojo.ContactsGroup;
import com.example.mapper.ContactsGroupMapper;
import com.example.pojo.ContactsGroupDetail;
import com.example.service.IContactsGroupService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.vo.ResultVo;
import com.example.vo.respvo.ContactsGroupVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 联系人分组 服务实现类
 * </p>
 *
 * @author wsl
 * @since 2022-05-14
 */
@Service
public class ContactsGroupServiceImpl extends ServiceImpl<ContactsGroupMapper, ContactsGroup> implements IContactsGroupService {

    @Autowired
    private ContactsGroupDetailMapper contactsGroupDetailMapper;

    @Override
    public ResultVo getUserContactsGroup(Integer empId) {
        List<Map<String,Object>> list = baseMapper.selectUserContactsGroups(empId);
        return ResultVo.response(list);
    }

    @Override
    public Boolean hasSameNameGroup(Integer id, String groupName) {
        QueryWrapper<ContactsGroup> wrapper = new QueryWrapper<>();
        wrapper.eq("group_name",groupName)
                .notIn(id!=null,"id",id);
        List<ContactsGroup> contactsGroups = baseMapper.selectList(wrapper);
        return contactsGroups.size() > 0;
    }

    @Override
    @Transactional
    public ResultVo deleteContactsGroup(Integer[] ids, Integer empId) {
        QueryWrapper<ContactsGroup> wrapper = new QueryWrapper<>();
        wrapper.in("id",Arrays.asList(ids))
                .eq("emp_id",empId);
        baseMapper.delete(wrapper);
        contactsGroupDetailMapper.delete(new QueryWrapper<ContactsGroupDetail>().in("contacts_group_id",Arrays.asList(ids)));
        return ResultVo.ok();
    }
}
